<!-- 发票明细 -->
<template>
  <el-dialog
    title="发票明细"
    :visible.sync="dialogVisible"
    append-to-body
    width="60%"
  >
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      v-model="form"
      ref="crud"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @row-save="rowSave"
      @row-update="rowUpdate"
      @row-del="rowDel"
      :before-open="beforeOpen"
      :upload-exceed="uploadExceed"
    >
      <template slot="zfptp" slot-scope="{ row }" v-if="row.zfptp">
        <el-image
          style="height: 50px; width: 50px; margin-right: 10px"
          fit="cover"
          :src="src"
          v-for="(src, index) in row.zfptp.split(';')"
          :key="index"
          :preview-src-list="row.zfptp.split(';')"
        >
          <div slot="error" style="line-height: 50px">
            <el-link type="primary" :href="src">下载</el-link>
          </div>
        </el-image>
      </template>
    </avue-crud>
    <span slot="footer" class="dialog-footer">
      <!-- <el-button
        type="primary"
        @click="submitForm"
        v-if="isSubmit && data.length > 0"
        >提 交</el-button
      > -->
      <el-button @click="dialogVisible = false">关 闭</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {
  getSettlementDetail,
  settlementSubmit,
  settlementRemove,
  submitSettlement,
} from "@/api/financial/index";

export default {
  data() {
    return {
      dialogVisible: false,
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      data: [],
      no: "",
      submit: 0, //提交状态
      form: {
        currencyType: "",
        total: "",
      },
      currencyType: "",
    };
  },
  components: {},
  computed: {
    //是否已提交
    isSubmit() {
      return this.submit === 0;
    },
    //是否有数据
    withData() {
      return this.page.total < 1;
    },
    option() {
      return {
        height: "auto",
        calcHeight: 200,
        tip: false,
        searchShow: false,
        searchMenuSpan: 6,
        border: true,
        columnBtn: false,
        addBtn: this.isSubmit && this.withData,
        menu: this.isSubmit,
        column: [
          {
            label: "发票日期",
            prop: "itemDate",
            type: "datetime",
            format: "yyyy-MM-dd HH:mm:ss",
            valueFormat: "yyyy-MM-dd HH:mm:ss",
            rules: [
              {
                required: true,
                message: "请选择发票日期",
                trigger: "change",
              },
            ],
          },
          {
            label: "发票号码",
            prop: "no",
            rules: [
              {
                required: true,
                message: "请输入发票号码",
                trigger: "blur",
              },
            ],
          },
          {
            label: "发票金额",
            prop: "price",
            // addDisplay: false,
            // editDisplay: false,
            rules: [
              {
                required: true,
                message: "请输入发票金额",
                trigger: "blur",
              },
            ],
          },
          {
            label: "发票税额",
            prop: "tax",
            // addDisplay: false,
            // editDisplay: false,
            rules: [
              {
                required: true,
                message: "请输入发票税额",
                trigger: "blur",
              },
            ],
          },
          {
            label: "价税合计",
            prop: "total",
            rules: [
              {
                required: true,
                message: "请输入价税合计",
                trigger: "blur",
              },
            ],
          },
          {
            label: "币别",
            prop: "currencyType",
            type: "select",
            disabled: true,
            props: {
              label: "name",
              value: "name",
            },
            dicUrl: `/api/blade-tms/billing/Settlement/currency`,
            rules: [
              {
                required: true,
                message: "请选择币别",
                trigger: "change",
              },
            ],
          },
          {
            label: "发票凭证",
            prop: "zfptp",
            type: "upload",
            span: 12,
            listType: "text",
            limit: 1,
            dataType: "string",
            propsHttp: {
              res: "data",
              name: "originalName",
              url: "link",
            },
            action: "/api/blade-resource/oss/endpoint/put-file-attach",
            rules: [
              {
                required: true,
                message: "请上传发票凭证",
                trigger: "change",
              },
            ],
          },
          {
            label: "是否含税",
            prop: "doesItIncludeTax",
            type: "radio",
            span: 12,
            // addDisplay: false,
            // editDisplay: false,
            dicData: [
              {
                label: "是",
                value: true,
              },
              {
                label: "否",
                value: false,
              },
            ],
            rules: [
              {
                required: true,
                message: "请选择是否含税",
                trigger: "blur",
              },
            ],
          },
        ],
      };
    },
  },
  methods: {
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
      this.getList(this.page);
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
      this.getList(this.page);
    },
    refreshChange() {
      this.getList(this.page);
    },
    open(no, submit, docCurrency, price) {
      this.dialogVisible = true;
      this.submit = submit;
      this.no = no;
      this.form.currencyType = docCurrency;
      this.form.total = price;
      this.currencyType = docCurrency;
      this.price = price;
      this.getList(this.page);
    },
    getList(page) {
      let query = {
        no: this.no,
      };
      this.loading = true;
      getSettlementDetail(page.currentPage, page.pageSize, query).then(
        (res) => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
        }
      );
    },
    beforeOpen(done, type) {
      if (type === "add") {
        this.form.currencyType = this.currencyType;
        this.form.total = this.price;
      }
      done();
    },
    rowSave(form, done, loading) {
      loading();
      settlementSubmit({ ...form, billingId: this.no }).then(
        ({ data }) => {
          if (data.code === 200 && data.success) {
            this.getList(this.page);
            this.$message({
              type: "success",
              message: "操作成功!",
            });
            done(form);
          }
        },
        (error) => {
          window.console.log(error);
          loading();
        }
      );
    },
    rowUpdate(form, index, done, loading) {
      loading();
      settlementSubmit({ ...form, billingId: this.no }).then(
        ({ data }) => {
          if (data.code === 200 && data.success) {
            this.getList(this.page);
            this.$message({
              type: "success",
              message: "操作成功!",
            });
            done(form);
          }
        },
        (error) => {
          window.console.log(error);
          loading();
        }
      );
    },
    rowDel(form) {
      this.$confirm("是否删除此发票?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          settlementRemove(form.id).then(({ data }) => {
            if (data.code === 200 && data.success) {
              this.getList(this.page);
              this.$message({
                type: "success",
                message: "操作成功!",
              });
            }
          });
        })
        .catch(() => {});
    },
    submitForm() {
      this.$confirm("提交后无法修改，是否确认提交发票明细?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          submitSettlement(this.no).then(({ data }) => {
            if (data.code === 200 && data.success) {
              this.$message({
                type: "success",
                message: "操作成功!",
              });
              this.$emit("refresh");
              this.dialogVisible = false;
            }
          });
        })
        .catch(() => {});
    },
    uploadExceed(limit, files, fileList) {
      this.$message.error(
        `最多上传${limit}个文件，已上传${fileList.length}个文件`
      );
    },
  },
};
</script>

<style></style>
